package com.tanhua.controller;

import com.tanhua.interceptor.UserHolder;
import com.tanhua.service.UserInfoService;
import model.domain.UserInfo;
import model.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户基础信息
     *
     * @param userId
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(Long userId) {
        String id = UserHolder.getUserId().toString();
        UserInfoVo userInfoVo = userInfoService.findById(userId == null ? Long.valueOf(id) : userId);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 更新用户资料
     *
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(Long.valueOf(UserHolder.getUserId()));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }
}
